package com.car.carcontrol;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.car.carcontrol.base.BaseFragment;

import pl.droidsonroids.gif.GifDrawable;
import pl.droidsonroids.gif.GifImageView;

//空调
public class Fragment_Sixth extends BaseFragment {
    public static String TAG = "6";

    private FrameLayout v;
    private Context context;

    private GifImageView iv_route;
    private GifDrawable gifDrawable;

    private TextView tv_temp;   //空调温度
    private TextView tv_wind;   //风速

    float temp = 20;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    Object[] allParam = new Object[]{

            new Object[]{"iv", new Rect(373*width/1280-width_d, 151*height/480-height_d, 777*width/1280, 285*height/480), R.drawable.border, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"gif", new Rect(431*width/1280-width_d, 174*height/480-height_d, 341*width/1280, 68*height/480), R.drawable.wind_1_gif, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"iv", new Rect(542*width/1280-width_d, 248*height/480-height_d, 190*width/1280, 191*height/480), R.drawable.temp_plate, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(597*width/1280-width_d, 303*height/480-height_d, 80*width/1280, 80*height/480), R.drawable.temp_round, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(602*width/1280-width_d, 308*height/480-height_d, 69*width/1280, 69*height/480), R.drawable.temp_round2, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(846*width/1280-width_d, 177*height/480-height_d, 241*width/1280, 241*height/480), R.drawable.wind_plate, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},


            new Object[]{"iv", new Rect(687*width/1280-width_d, 331*height/480-height_d, 20*width/1280, 26*height/480), R.drawable.plus, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(567*width/1280-width_d, 331*height/480-height_d, 20*width/1280, 26*height/480), R.drawable.reduce, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

//            20℃
            new Object[]{"tv", new Rect(609*width/1280-width_d, 334*height/480-height_d, 0, 0), "温度", 10, Color.WHITE, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"tv", new Rect(931*width/1280-width_d, 344*height/480-height_d, 0, 0), "1", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(891*width/1280-width_d, 310*height/480-height_d, 0, 0), "2", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(891*width/1280-width_d, 254*height/480-height_d, 0, 0), "3", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(933*width/1280-width_d, 223*height/480-height_d, 0, 0), "4", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(982*width/1280-width_d, 223*height/480-height_d, 0, 0), "5", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(1022*width/1280-width_d, 255*height/480-height_d, 0, 0), "6", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(1022*width/1280-width_d, 310*height/480-height_d, 0, 0), "7", 20, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"tv", new Rect(969*width/1280-width_d, 351*height/480-height_d, 0, 0), "AUTO", 12, Color.WHITE, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},

            new Object[]{"tv", new Rect(958*width/1280-width_d, 282*height/480-height_d, 0, 0), "风速", 20, Color.WHITE, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_Sixth===0", v+"==="+width_d+"==="+height_d);

        initView();

        isThread = true;
        loopRead();

        return v;
    }

    @Override
    public void onDestroy() {
        Log.e("f6===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);

                if("温度".equals(tv.getText())){
                    tv_temp = tv;
                    temp = 20;
                    tv_temp.setText(temp+"℃");
                }else if("风速".equals(tv.getText())){
                    tv_wind = tv;
//                    temp = 20;
                    tv_wind.setText("4");
                }

                boolean isCanClick = (boolean)obj[5];

                Log.e("tv===", name+"==="+isCanClick);

                if(isCanClick){
                    tv.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===", v+"===");

                            String name = (String)obj[2];
//                            String type2 = (String)obj[5];
//                            byte[] bytes = (byte[])obj[4];

                            tv_wind.setText(name);

                            if("1".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_1_gif);
                                gifDrawable.start();
                            }else if("2".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_2_gif);
                                gifDrawable.start();
                            }else if("3".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_3_gif);
                                gifDrawable.start();
                            }else if("4".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_4_gif);
                                gifDrawable.start();
                            }else if("5".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_5_gif);
                                gifDrawable.start();
                            }else if("6".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_6_gif);
                                gifDrawable.start();
                            }else if("7".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_7_gif);
                                gifDrawable.start();
                            }else if("AUTO".equals(name)){
                                iv_route.setImageResource(R.drawable.wind_auto_gif);
                                gifDrawable.start();
                            }

//                            write(bytes); //TODO
                        }
                    });

                    tv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {


                            return false;
                        }
                    });
                }
            }else if("gif".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                iv_route = new GifImageView(context);
                iv_route.setImageResource(imgSrc);
                iv_route.setScaleType(ImageView.ScaleType.FIT_XY);
                gifDrawable = (GifDrawable) iv_route.getDrawable();
                gifDrawable.stop();

                v.addView(iv_route, params);
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.drawable.temp_round2){
                    iv.setVisibility(View.GONE);
                }

                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){

                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction());

                            int imgId = (int)obj[2];
//                            String type2 = (String)obj[5];
//                            byte[] bytes = (byte[])obj[4];

                            if(event.getAction()==1){
                                iv.setImageResource(imgId);

                                if(imgId==R.drawable.plus){
                                    temp += 0.5;
                                    tv_temp.setText(temp+"℃");
                                }else if(imgId==R.drawable.reduce){
                                    temp -= 0.5;
                                    tv_temp.setText(temp+"℃");
                                }



                            }else{
                                if(imgId==R.drawable.plus){
                                    iv.setImageResource(R.drawable.plusf);
                                }else if(imgId==R.drawable.reduce){
                                    iv.setImageResource(R.drawable.reducef);
                                }
                            }

//                            write(bytes); //TODO

                            return false;
                        }
                    });

                    iv.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            Log.e("onClick===iv", v+"===");

                            int imgId = (int)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            if(imgId==R.drawable.temp_round){
                                iv.setImageResource(R.drawable.temp_round2);
                                obj[2] = R.drawable.temp_round2;

//                                read(bytes);  //TODO
                            }else if(imgId==R.drawable.temp_round2){
                                iv.setImageResource(R.drawable.temp_round);
                                obj[2] = R.drawable.temp_round;
                            }
                        }
                    });
                }

            }
        }
    }

    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==5){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO

                    Log.e("f6===", "===");

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

}

